import { computed, ref } from 'vue';

import { defineStore, storeToRefs } from 'pinia';

import { purchaseGoodsAndPayApi } from '#/api';
import { auto1688Store } from '#/store/modules/1688Auto';

export const typeList = [
  {
    type: '1',
    name: '批量下单并支付',
  },
  {
    type: '2',
    name: '批量下单（不支付）',
  },
  {
    type: '3',
    name: '批量支付',
  },
  {
    type: '4',
    name: '合并下单并支付',
  },
];
export const payOrderStore = defineStore('payOrder', () => {
  const auto1688 = auto1688Store();
  const { bindShopList } = storeToRefs(auto1688);
  const getList = () => {};

  const nowUse1688Shop = computed(() => {
    // console.log('修改后的信息', bindShopList);
    return bindShopList.value.find(
      (item) => item.pType === 1 && item.isUseing === 1 && item.status === 1,
    );
  });
  const selectItemInfos = ref([]);
  const payType = ref(''); // 支付方式
  const payTypeName = ref(''); // 支付方式名称
  const inLine = ref([]);

  const upPterLodin = ref(false);

  function $reset() {
    selectItemInfos.value = [];
    payType.value = '';
    payTypeName.value = '';
  }
  function setPayType(data: any) {
    payType.value = data;
    const type = typeList.find((item) => item.type === data);
    payTypeName.value = type?.name || '';
  }

  function setSelectItemInfos(data: any, paytype: string) {
    // payType.value = paytype;

    setPayType(paytype);
    selectItemInfos.value = data.map((item: any) => {
      const pyobj = {
        pytstyas: 3, // 0 未关联货源 1 订单不是待支付状态 2 订单不是待采购状态 3 可以操作 4 5 6 7 支付成功 失败 等信息 待发货等
        pytstr: '',
      };
      const pystyres = {
        status: 0, // 等待 1 成功 2 失败
        msg: pyobj.pytstr,
      };
      item.pystyres = pystyres;
      if (!item.offerId) {
        pyobj.pytstyas = 0;
        pyobj.pytstr = '未关联货源';
        item.pyobj = pyobj;
        return item;
      }
      if (
        (paytype === '3' && item.aliOrderStatus !== 'waitbuyerpay') ||
        item.dyOrderStatus !== 2
      ) {
        pyobj.pytstyas = 1;
        pyobj.pytstr = '订单不是待支付状态';
        item.pyobj = pyobj;
        return item;
      }
      if (
        paytype !== '3' &&
        item.aliOrderStatus &&
        item.aliOrderStatus !== 'cancel'
      ) {
        pyobj.pytstyas = 2;
        pyobj.pytstr = '订单不是待采购状态';
        item.pyobj = pyobj;
        return item;
      }
      if (paytype === '1') {
        pyobj.pytstyas = 3;
        pyobj.pytstr = '等待下单并支付';
        item.pyobj = pyobj;
        return item;
      }
      if (paytype === '2') {
        pyobj.pytstyas = 3;
        pyobj.pytstr = '等待下单';
        item.pyobj = pyobj;
        return item;
      }
      if (paytype === '3') {
        pyobj.pytstyas = 3;
        pyobj.pytstr = '等待支付';
        item.pyobj = pyobj;
        return item;
      }

      item.pyobj = pyobj;
      return item;
    });
    setinLine();
  }
  function setinLine() {
    inLine.value = selectItemInfos.value.filter(
      (item: any) => item.pyobj.pytstyas === 3 && item.pystyres.status === 0,
    );
  }

  function removeSelectItemInfo(data: any) {
    selectItemInfos.value = selectItemInfos.value.filter(
      (item: any) => item.tbId !== data.tbId,
    );
  }

  function purchaseGoodsFun() {
    return new Promise((resolve) => {
      upPterLodin.value = true;
      purchaseGoodsAndPayApi({
        tbIds: inLine.value.map((item: any) => item.tbId),
        types: Number(payType.value),
      })
        .then((res) => {
          upPterLodin.value = false;
          // console.log('res', res);
          res.forEach((item: any) => {
            const findOuterId: any = selectItemInfos.value.find(
              (i: any) => i.tbId === item.tbId,
            );
            findOuterId.pystyres = item.res
              ? {
                  status: 1,
                  msg: '成功',
                }
              : {
                  status: 2,
                  msg: item.msg,
                };
          });
          setinLine();
          resolve(res);
        })
        .catch((error) => {
          upPterLodin.value = false;
          throw error;
        });
    });
  }
  return {
    $reset,
    selectItemInfos,
    setSelectItemInfos,
    payType,
    setPayType,
    typeList,
    payTypeName,
    nowUse1688Shop,
    removeSelectItemInfo,
    inLine,
    purchaseGoodsFun,
    upPterLodin,
    getList,
  };
});
